<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
    	<%@ page import="servlets.category.*"%>
<%@ page import="org.hibernate.SessionFactory"%>
<%@ page import="org.hibernate.Session"%>
<%@ page import="java.util.List"%>
<%@ page import="hibernate.model.*"%>
<%@ page import="hibernate.util.HibernateUtil"%>
<%@ page import="hibernate.method.*"%>
<%@ page import="hibernate.method.CategoryMethods"%>

<script>
$(document).ready(function() {

$('#game1').change(function(event) {
        var game1 = $("select#game1").val();
        $.get('../deleteGameCat', {
                game1 : game1
        }, function(response) {

        var select = $('#category1');
        select.find('option').remove();
          $.each(response, function(index, value) {
          $('<option>').val(value).text(value).appendTo(select);
      });
        });
        });

$('#category').change(function(event) {
        var category = $("select#category").val();
        $.get('../addGameCat', {
                category : category
        }, function(response) {
        var select = $('#game');
        select.find('option').remove();
          $.each(response, function(index, value) {
          $('<option>').val(value).text(value).appendTo(select);
      });
        });
        });
});
</script>

<div class="page-header">
	<h2 class="page-title">Dodaj kategorie do gry:</h2>
</div>

<br />

<div id="content">
<%
						SessionFactory sessionFactory = HibernateUtil
								.getSessionAnnotationFactory();
						Session s = sessionFactory.openSession();
						s.beginTransaction();	
						GameMethods game = new GameMethods();
						CategoryMethods catm = new CategoryMethods();
						List <Category> cat = catm.listAllCategories(sessionFactory, s);
						List<Game> games = game.listAllGames(sessionFactory, s);
					%> 
					
<form action="../addGameCat" method="POST" name='formularz'>
<table>
	<tr>
			<td>
			Kategoria gry:
			</td><td>
			<select id="category" name="category">
						<option value="0" selected>Wybierz kategorie:</option>
						<%	if (cat.size() > 0) {
							for (Category c : cat) {
								String catname = c.getCategory();
								int id = c.getCategory_id();%>
						<option value="<%=id%>"><%=catname%></option>
						<%}
							
						}
						if (cat.size() == 0) {
					 out.println("Nie ma zadnych kategorii w bazie");
					
						} %>
						</select>
			</td>
			</tr> 
			<tr>
			<td>
			Gra:
			</td>
			<td>
			<select id="game" name="game">
						<option value="0" selected>Wybierz gre:</option>
						</select>
						</td>
						</tr>
							<tr>
				<td><input type="submit" class="mainBtn" value="Dodaj kategorie do gry" /></td>
			</tr>
			</table>
			</form>
			
			
						<div class="page-header">
<h2 class="page-title">usun kategorie z gry:</h2>
</div>


			
			<form action="../deleteGameCat" method="POST" name='for'>
<table>
	<tr>
			<td>
			Gra:
			</td>
			<td>
			<select id="game1" name="game1">
						<option value="0" selected>Wybierz gre:</option>
						<%	if (games.size() > 0) {
							for (Game g : games) {
								String gamename = g.getName();
								int id = g.getGame_id();%>
						<option value="<%=id%>"><%=gamename%></option>
						<%}
							
						}
						if (games.size() == 0) {
					 out.println("Nie ma zadnych kategorii w bazie");
					
						} %>
						</select>
						</td>
						</tr>
						
						<tr>
						<td>
						Kategoria do usuniecia:
						</td>
						<td>
						<select id="category1" name="category1">
						<option value="0" selected>Wybierz kategorie gry:</option>
						</select>
				
						</td>
						
						
							<tr>
				<td><input type="submit" class="mainBtn" value="Usun kategorie z gry" /></td>
			</tr>
			</table>
			</form>
			
			
				<% 	s.getTransaction().commit(); %>
</div>